// pages/video/video.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shareTipsShow: false,
        nickname: '',
        videoList: {},
        like_status: '',
        user_video_id:'',
        activity_id:'',
        isLogin: false,
        showCoverImg: true,
    },
    // 关闭弹窗
    closeShareTips: function() {
        this.setData({
            shareTipsShow: false
        })
    },
    // 点赞弹窗
    priaseCount: function() {
        this.setData({
            shareTipsShow: true
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.data.use_video_id = options.use_video_id
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onShow: function() {
        this.getVideoDetail()
        this.getauth = this.selectComponent('#getauth')
        let sign = wx.getStorageSync('sign')
        let that = this
        if (!sign) {
            app.wxLogin(jscode => {
                app.globalData.jscode = jscode
                wx.getSetting({
                    success(settingRes) {
                        if (!settingRes.authSetting['scope.userInfo']) {
                            that.getauth.showAuth()
                        } else {
                            // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                            wx.getUserInfo({
                                withCredentials: true,
                                success: function (getUserInfoRes) {
                                    app.globalData.userInfo = getUserInfoRes.userInfo
                                    app.globalData.encryptedData = getUserInfoRes.encryptedData
                                    app.globalData.iv = getUserInfoRes.iv
                                }
                            })
                        }
                    }
                })
            })
        }else{
            this.setData({
                isLogin: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onReady: function() {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    // 登录
    login: function () {
        wx.navigateTo({
            url: '/pages/userLogin/userLogin',
        })
    },
    // 获取视频详情页数据
    getVideoDetail: function() {
        var sign = wx.getStorageSync('sign') || ''
        var that = this
        wx.request({
            url: app.globalData.url + '/videolike/getVideoDetail?sign=' + sign + '&user_video_id=' + that.data.use_video_id,
            success: function(msg) {
                if (msg.data.code == 200) {
                    let resData = msg.data.data;
                    if (resData.like_num && resData.like_num > 1000) {
                        resData.like_num = app.numChange(resData.like_num)
                    }
                    that.setData({
                        videoList: resData,
                        like_status: msg.data.data.like_status,
                        user_video_id: msg.data.data.user_video_id,
                        activity_id: msg.data.data.activity_id
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: msg.data.message,
                        showCancel: false
                    })
                }
            },
            fail: () => {
                wx.showModal({
                    title: '提示',
                    content: '服务器错误，请稍后再试',
                    showCancel: false
                })
            }
        })
    },
    // 点赞功能
    getPrise:function(){
        var sign = wx.getStorageSync('sign')
        var that = this;
        if (sign){
            let data = {
                'user_video_id': that.data.user_video_id,
                'aid': that.data.activity_id
            }
            wx.request({
                url: app.globalData.url + 'Videolike/addVideoLikeNum?sign=' + sign,
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                data: data,
                success: msg => {
                    if (msg.data.code == 200) {
                        wx.showToast({
                            title: '点赞成功',
                        })
                        that.getVideoDetail()
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: msg.data.message,
                            showCancel: false
                        })
                    }
                },
                fail: () => {
                    wx.showModal({
                        title: '提示',
                        content: '服务器错误，请稍后再试',
                        showCancel: false
                    })
                }
            })
        }else{
            wx.navigateTo({
                url: '/pages/userLogin/userLogin',
            })
        }
        
    },
    // 重新上传视频
    uploadVideo:function() {
        let that = this
        app.globalData.user_video_id = that.data.user_video_id
        app.chooseVideo(() => {
            that.getVideoDetail()
        })
    },
    //登录成功回调
    _loginSucess() {
        this.setData({
            isLogin: true
        })
        this.getVideoDetail()
    },
    // 播放视频
    playVideo:function() {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        this.setData({
            shareTipsShow: false
        })
        let that = this
        let  path = '/pages/video/video?use_video_id=' + that.data.use_video_id 
        return {
            title: that.data.videoList.nickname + '的演讲作品',
            path:path,
            success: function (res) {

            }
        }
    }
})